package com.duoduo.admin.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.duoduo.common.core.constant.CacheConstants;
import com.duoduo.common.core.util.Page;
import com.duoduo.common.core.util.PageRequest;
import com.duoduo.common.core.util.R;
import com.duoduo.common.security.service.DuoUser;
import com.duoduo.common.security.util.SecurityUtils;

import cn.hutool.core.lang.tree.Tree;

import com.duoduo.admin.api.dto.SysMenuDTO;
import com.duoduo.admin.service.SysMenuService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpHeaders;

@RestController
@RequestMapping("menu")
@Tag(name = "系统菜单表接口", description ="系统菜单表访问接口")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysMenuController {
	
	@Autowired
	private SysMenuService sysMenuService;
	
	/**
	 * 分页
	 * @param request
	 * @return
	 */
	@PostMapping("page")
	@Operation(summary = "分页")
	public R<Page<SysMenuDTO>> page(PageRequest<SysMenuDTO> request){
		Page<SysMenuDTO> page = sysMenuService.page(request.getPage(),request.getPageSize(),request.getParam());
		return R.ok(page);
	}
	/**
	 * 详情
	 * @param id
	 * @return
	 */
	@GetMapping("{id}/get")
	@Operation(summary = "详情")
	public R<SysMenuDTO> get(@Parameter(description = "主键id") @PathVariable("id")Long id){
		return R.ok(sysMenuService.get(id));
	}
	/**
	 * 创建
	 * @param id
	 * @return
	 */
	@Operation(summary = "创建")
	@PostMapping("create")
	@CacheEvict(value = CacheConstants.MENU_DETAILS, allEntries = true)
	public R<SysMenuDTO> create(@RequestBody SysMenuDTO request){
		sysMenuService.create(request);
		return R.ok();
	}
	/**
	 * 修改
	 * @param id
	 * @return
	 */
	@Operation(summary = "修改")
	@PutMapping("update")
	@CacheEvict(value = CacheConstants.MENU_DETAILS, allEntries = true)
	public R<SysMenuDTO> update(@RequestBody SysMenuDTO request){
		sysMenuService.update(request,request.getId());
		return R.ok();
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Operation(summary = "删除")
	@DeleteMapping("{id}/delete")
	@CacheEvict(value = CacheConstants.MENU_DETAILS, allEntries = true)
	public R<SysMenuDTO> delete(@Parameter(description = "主键id") @PathVariable("id")Long id){
		sysMenuService.delete(id);
		return R.ok();
	}
	/**
	 * 菜单列表
	 * @param id
	 * @return
	 */
	@GetMapping("treeMenu")
	@Operation(summary = "菜单列表")
	public R<List<Tree<Long>>> treeMenu(@RequestParam( required=true, defaultValue = "true")Boolean lazy, @RequestParam( required=false) Long parentId){
		return R.ok(sysMenuService.treeMenu(lazy,parentId));
	}
	/**
	 * 菜单列表
	 * @param id
	 * @return
	 */
	@GetMapping("list")
	@Operation(summary = "菜单列表")
	public R<List<Tree<Long>>> list(){
		return R.ok(sysMenuService.list());
	}
	/**
	 * 个人菜单列表
	 * @param id
	 * @return
	 */
	@GetMapping("userMenu")
	@Operation(summary = "个人菜单列表")
	public R<List<Tree<Long>>> userMenu(){
		DuoUser user = SecurityUtils.getUser();
		return R.ok(sysMenuService.userMenu(user.getId()));
	}

	
}
